package ro.gateway.aida.obj;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Vector;



/**
 * <p>Title: Romanian AIDA</p>
 * <p>Description: </p>
 * <p>Copyright: Copyright (comparator) 2003</p>
 * <p>Company: ro-gateway</p>
 * @author Mihai Popoaei, mihai_popoaei@yahoo.com, smike@intellisource.ro
 * @version 1.0-* @version $Id: PersonInv.java,v 1.1 2004/10/24 23:37:10 mihaipostelnicu Exp $
 * de ce folosesc obiectul PersonInv cand pot folosi
 * Person? pt. forma de editare... sa grupez persoanele
 * dupa rolul organizatiei din care face parte, rol
 * in activitatea care se editeaza :P
 *
 */
public class PersonInv extends AIDAActivityObject
{
      private long person_id;
      private Person person;
      public OrganizationRole role;

      public PersonInv( Person person, Integer local_role_id )
      {
            this.role = OrganizationRole.getCachedByLocalId( local_role_id );
            if ( this.role == null )
                  throw new RuntimeException( "Unsupported role :D" );

            this.setPerson( person );
            this.objectType = TYPE_PERSON_INVOLVED;
      }

      public PersonInv()
      {
            this.objectType = TYPE_PERSON_INVOLVED;
      }

      public PersonInv( Person person, OrganizationRole role )
      {
            this.role = role;
            this.setPerson( person );
            this.objectType = TYPE_PERSON_INVOLVED;
      }

      public void setPerson( Person person )
      {
            this.person = person;
            if ( person != null )
            {
                  this.person_id = person.getId();
            }
            else
            {
                  this.person_id = -1;
            }
      }

      public void setPerson_id( long person_id )
      {
            this.person_id = person_id;
      }

      public Person getPerson()
      {
            return person;
      }

      public long getPerson_id()
      {
            return person_id;
      }

      public static Vector buildPersInvVector( PersonInv[] items )
      {
            Vector result = new Vector();
            if ( items == null )
            {
                  return result;
            }

            for ( int i = 0; i < items.length; i++ )
            {
                  result.add( items[ i ].getPerson() );
            }

            return result;
      }

      /**
       * Primeste un Vector de obiecte Organizatie si returneaza
       * un array de OrganizationInv
       * @param persons
       * @return
       */
      public static PersonInv[] buildPersInvArray( List persons, Integer local_role_id )
      {
            PersonInv result[] = null;

            if ( persons == null ) return result;
            OrganizationRole role = OrganizationRole.
                  getCachedByLocalId( local_role_id );
            if ( role == null )
            {
                  System.err.println( "buildPersInvArray: can't find role:" + local_role_id );
                  return result;
            }

            ArrayList items = new ArrayList();
            Iterator i=persons.iterator();
            while(i.hasNext())
            {
                  Object o = i.next();
                  if ( o instanceof Person )
                  {
                        PersonInv org = new PersonInv( ( Person ) o, role );
                        items.add( org );
                  }
            }

            if ( items.size() > 0 )
            {
                  result = new PersonInv[ items.size() ];
                  items.toArray( result );
            }

            return result;
      }
	/**
	 * @return
	 */
	public OrganizationRole getRole() {
		return role;
	}

	/**
	 * @param role
	 */
	public void setRole(OrganizationRole role) {
		this.role = role;
	}

}
